//
//  JiongPictureViewController.m
//  JiuwanBox
//
//  Created by lanou3g on 15/5/25.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "JiongPictureViewController.h"
#import "ShowVieController.h"
#import "UIImageView+WebCache.h"
@interface JiongPictureViewController ()
{
    UICollectionView * collectView;
}
@property(nonatomic,strong)NSMutableArray *dataArray;

@end



@implementation JiongPictureViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its
    self.navigationItem.titleView = [[UIImageView    alloc]initWithImage:[UIImage imageNamed:@"12356.jpg"]];
    self.navigationController.navigationBar.tintColor = [UIColor  colorWithRed:0.3 green:0.4 blue:0.5 alpha:1];
    self.dataArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor blackColor];
    
    [self read];//解析数据
    [self creatCollectionView];//创建CollectionView
    
    
 
    
}


//创建CollectionView
-(void)creatCollectionView
{
    WaterFlowLayout *layout=[[WaterFlowLayout alloc]init];
    layout.itemSize=CGSizeMake(140, 230);
    layout.interitemSpacing=10;
    layout.lineSpacing=10;
    layout.sectionInsets=UIEdgeInsetsMake(10, 10, 40, 10);
    layout.numberOfColumns=2;
    layout.delegate=self;
    
    
    collectView=[[UICollectionView  alloc]initWithFrame:self.view.frame collectionViewLayout:layout];
    collectView.dataSource=self;
    collectView.delegate=self;
    collectView.backgroundColor = [UIColor colorWithRed:0.8 green:0.9 blue:0.9 alpha:1];
    [self.view addSubview:collectView];
    
//    [collectView registerClass:[MyViewCell class] forCellWithReuseIdentifier:@"cell"];
    [collectView registerNib:[UINib nibWithNibName:@"MyViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
}

//解析数据
-(void )read
{
        NSMutableArray * allArray = [[NSMutableArray alloc]init];
        for (int i =15; i<=74; i++) {
            NSString *string = [NSString  stringWithFormat:@"http://box.dwstatic.com/apiAlbum.php?action=l&albumsTag=beautifulWoman&p=%d",i];
            NSURL *url = [NSURL URLWithString:string];
            NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
            
            [NSURLConnection  sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                
                NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                NSArray * sourceArray = [dic  objectForKey:@"data"];
                
                for (NSDictionary *dic  in sourceArray) {
                    ItemModel *model=[[ItemModel alloc]init];
                    [model setValuesForKeysWithDictionary:dic];
                    [allArray addObject:model];
                }
                self.dataArray = [NSMutableArray arrayWithArray:allArray];
                NSLog(@"%lu",(unsigned long)_dataArray.count);
                
                //在主线程中，显示下载的图像
                dispatch_async(dispatch_get_main_queue(), ^{
                    
                    [collectView reloadData];
                    
                });
             }];
        }

}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
   
    return self.dataArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MyViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    
    ItemModel *model=self.dataArray[indexPath.item];
    NSURL *url=[NSURL URLWithString:model.coverUrl];
    //下载图片
    
    [cell.imageView sd_setImageWithURL:url];
    NSString *string = [NSString stringWithFormat:@"共%d张",model.picsum];
    cell.label.text =string;
    cell.titleLabel.text = model.title;
    
    cell.imageView.frame = CGRectMake(0, 0, 140, 140*model.coverHeight/model.coverWidth);
    cell.titleLabel.frame = CGRectMake(0, 140*model.coverHeight/model.coverWidth, 140, 30);
    cell.label.frame = CGRectMake(0, 140*model.coverHeight/model.coverWidth - 20, 140, 20);
    return cell;
}

//根据每一个item的model数据,计算item的高度
-(CGFloat)heightForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemModel *model=self.dataArray[indexPath.item];
    CGFloat itemWidth=140;
    CGFloat itemHeight=itemWidth*model.coverHeight/model.coverWidth  +40;

    
    return itemHeight;
}

//推出详情界面,并将tabbar隐藏
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ItemModel *model=self.dataArray[indexPath.item];
    ShowVieController *showVC = [[ShowVieController alloc]init];
    showVC.model = model ;
    self.parentViewController.tabBarController.tabBar.hidden = YES;
    [self.navigationController  pushViewController:showVC animated:YES];
}


//页面将要出现时,把tabbar隐藏属性关掉,显示tabbar
-(void)viewWillAppear:(BOOL)animated
{
    self.parentViewController.tabBarController.tabBar.hidden = NO;

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
